// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: daniel

#pragma once

#include <memory>
#include <string>
#include <unordered_map>

#include "coin2/exchange/base/api_base/asio_http_context.h"
#include "coin2/exchange/base/order/order.h"
#include "coin2/exchange/coinone/api_util/auth_key.h"
#include "coin2/exchange/coinone/symbology/product.h"

namespace coin2::exchange::order {
class CoinoneRestParams;
class OrderConnectionConfig;
}  // namespace coin2::exchange::order

namespace coin2::exchange::base::order {
struct OrderExecution;
struct OrderSpec;
}  // namespace coin2::exchange::base::order

class CoinoneNativePrivateClient : public coin2::exchange::base::order::NativePrivateClient {
 public:
  using Base = coin2::exchange::base::order::NativePrivateClient;
  using RestParams = coin2::exchange::order::CoinoneRestParams;
  using NativeClientConfig = coin2::exchange::order::OrderConnectionConfig;
  using OrderExecution = coin2::exchange::base::order::OrderExecution;
  using OrderSpec = coin2::exchange::base::order::OrderSpec;
  using IOrderContext = coin2::exchange::base::order::IOrderContext;
  using HttpContextPtr = std::shared_ptr<AsioHttpContext>;
  using ActionType = coin2::exchange::base::order::NativePrivateClient::ActionType;
  using SymbolMap = std::unordered_map<std::string, std::string>;
  using CoinoneProduct = coin2::exchange::coinone::symbology::CoinoneProduct;

 public:
  std::shared_ptr<AsioHttpContext> SubmitOrderExecution(const OrderExecutionProto& order);
  std::shared_ptr<AsioHttpContext> SubmitRestQuery(const RestQueryProto& query);

 public:
  explicit CoinoneNativePrivateClient(
      const MarketExchangeApi& mea,
      const NativeClientConfig& config);

  HttpContextPtr PlaceOrder(const RestParams& params);
  HttpContextPtr CancelOrder(const RestParams& params);

  HttpContextPtr QueryAccountBalance();
  HttpContextPtr QueryLimitOrders(const RestParams& params);
  HttpContextPtr QueryCompleteOrders(const RestParams& params);
  HttpContextPtr QueryOrderInfo(const RestParams& params);

  HttpContextPtr PlaceOrderNorm(const OrderExecution&) override;
  HttpContextPtr CancelOrderNorm(const OrderExecution&) override;

 private:
  RestParams GetRestOrderParams(const OrderSpec& order_spec, int64_t proc_order_id);
  RestParams GetRestCancelParams(const IOrderContext& order_context);

 private:
  const std::string path_query_account_balance_ = "/v2/account/balance/";
  const std::string path_query_limit_orders_ = "/v2/order/limit_orders/";
  const std::string path_query_complete_orders_ = "/v2/order/complete_orders/";
  const std::string path_query_order_info_ = "/v2/order/order_info/";
  const std::string path_limit_buy_order_ = "/v2/order/limit_buy/";
  const std::string path_limit_sell_order_ = "/v2/order/limit_sell/";
  const std::string path_cancel_order_ = "/v2/order/cancel/";
};
